import {baseUrl} from './env'

export default async(url = '', data = '', type = 'GET', method = 'fetch') => {
						type = type.toUpperCase();//把字符串转化成大写
						url = baseUrl + url;

						let dataStr = ''; //数据拼接字符串
						Object.keys(data).forEach(key => {
							dataStr += key + '=' + data[key] + '&';
						})
						if (type == 'GET') {
							if (dataStr !== '') {
								dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
								url = url + '.shtml?sessionid='+sessionStorage.sid +'&'+ dataStr ;
							}
						}else{
							if(dataStr !== ''){
								dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
							}
							if(sessionStorage.sid){
								url = url+'.shtml?sessionid='+sessionStorage.sid
							}else{
								// alert('请重新登录！')
							}
						}

						// 如果浏览器支持fetch使用fetch，如果不支持使用ajax
						if (window.fetch && method == 'fetch') {

							let requestConfig = {
								method: type,
								headers: {
									"Content-Type": "application/x-www-form-urlencoded"
								},
								mode: "cors"
							}

							if (type == 'POST') {
								Object.defineProperty(requestConfig, 'body', {
									value: dataStr
								})
							}
							
							try {
								const response = await fetch(url, requestConfig);
								const responseData = response.json();
								return responseData;
							} catch (error) {
								throw new Error(error)
							}
						}else{//ajax
							return new Promise((resolve, reject) => {
								let requestObj;
								if (window.XMLHttpRequest) {
									requestObj = new XMLHttpRequest();
								} else {
									requestObj = new ActiveXObject;
								}

								let sendData = '';
								if (type == 'POST') {
									sendData = dataStr;
								}

								requestObj.open(type, url, true);
								requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
								requestObj.send(sendData);

								requestObj.onreadystatechange = () => {
									if (requestObj.readyState == 4) {
										if (requestObj.status == 200) {
											let obj = requestObj.response
											if (typeof obj !== 'object') {
												obj = JSON.parse(obj);
											}
											resolve(obj)
										} else {
											reject(requestObj)
										}
									}
								}
							})
						}
					}
//登录专用
export let fetchLogin = async(url = '', data = '', type = 'POST', method = 'fetch') => {
						type = type.toUpperCase();//把字符串转化成大写
						url = baseUrl + url;

						// 如果浏览器支持fetch使用fetch，如果不支持使用ajax
						if (window.fetch && method == 'fetch') {

							let requestConfig = {
								method: type,
								headers: {
									"Content-Type": "application/x-www-form-urlencoded"
								},
								mode: "cors"
							}

							
							Object.defineProperty(requestConfig, 'body', {})
							
							
							try {
								const response = await fetch(url, requestConfig);
								const responseData = response.json();
								return responseData;
							} catch (error) {
								throw new Error(error)
							}
						}else{//ajax
							return new Promise((resolve, reject) => {
								let requestObj;
								if (window.XMLHttpRequest) {
									requestObj = new XMLHttpRequest();
								} else {
									requestObj = new ActiveXObject;
								}

								let sendData = '';
								

								requestObj.open(type, url, true);
								requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
								requestObj.send(sendData);

								requestObj.onreadystatechange = () => {
									if (requestObj.readyState == 4) {
										if (requestObj.status == 200) {
											let obj = requestObj.response
											if (typeof obj !== 'object') {
												obj = JSON.parse(obj);
											}
											resolve(obj)
										} else {
											reject(requestObj)
										}
									}
								}
							})
						}
					}

export let fetchJson =  async(url = '', data = {}, type = 'POST', method = 'fetch') => {
							type = type.toUpperCase();//把字符串转化成大写
							url = baseUrl + url;

							if (window.fetch && method == 'fetch') {

								let requestConfig = {
									method: type,
									headers: {
										'Accept': 'application/json',
										'Content-Type': 'application/json'
									},
									mode: "cors",
								}
								
								Object.defineProperty(requestConfig, 'body', {
									value: JSON.stringify(data)
								})
								
								
								try {
									const response = await fetch(url, requestConfig);
									const responseJson = await response.json();
									return responseJson
								} catch (error) {
									throw new Error(error)
								}
							} else {
								return new Promise((resolve, reject) => {
									let requestObj;
									if (window.XMLHttpRequest) {
										requestObj = new XMLHttpRequest();
									} else {
										requestObj = new ActiveXObject;
									}

									let sendData = '';
									
									sendData = JSON.stringify(data);
									

									requestObj.open(type, url, true);
									requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
									requestObj.send(sendData);

									requestObj.onreadystatechange = () => {
										if (requestObj.readyState == 4) {
											if (requestObj.status == 200) {
												let obj = requestObj.response
												if (typeof obj !== 'object') {
													obj = JSON.parse(obj);
												}
												resolve(obj)
											} else {
												reject(requestObj)
											}
										}
									}
								})
							}
						}
//formdata
export let fetchForm =  async(url = '', data = {}, type = 'POST', method = 'fetch') => {
							type = type.toUpperCase();//把字符串转化成大写
							url = baseUrl + url+'.shtml?sessionid='+sessionStorage.sid;

								return new Promise((resolve, reject) => {
									let requestObj;
									if (window.XMLHttpRequest) {
										requestObj = new XMLHttpRequest();
									} else {
										requestObj = new ActiveXObject;
									}
									

									requestObj.open(type, url, true);
									requestObj.setRequestHeader("X-Requested-With","XMLHttpRequest");
									requestObj.send(data);

									requestObj.onreadystatechange = () => {
										if (requestObj.readyState == 4) {
											if (requestObj.status == 200) {
												let obj = requestObj.response
												if (typeof obj !== 'object') {
													obj = JSON.parse(obj);
												}
												resolve(obj)
											} else {
												reject(requestObj)
											}
										}
									}
								})
						}
